using System;
using System.Collections.Generic;
using UnityEngine;

//mao 24-11-23 02:46
//ADD：设置监听事件的默认开启和销毁
//ADD：添加可以设定等待时间
namespace Light
{
    /// <summary>
    ///     可以重复指定倒计时时间
    ///     <para>一开始需要手动指定一个倒计时时间</para>
    ///     <para>直到最后调用RemoveActionListener（）方法为止</para>
    /// </summary>
    public class Timer
    {
        private float duration;

        private bool isActive;

        private float startTime;

        private float targetTime;


        /// <summary>
        ///     new一个监听的event
        /// </summary>
        /// <param name="onTimerDone"></param>
        public Timer(Action onTimerDone)
        {
            OnTimerDone = onTimerDone;
            //实践触发默认启动
            MonoManager.Instance.AddUpdateListener(Tick);
        }

        public event Action OnTimerDone;

        /// <summary>
        ///     在StartTimer之前一定要被调用
        /// </summary>
        /// <param name="duration"></param>
        public void SetDurationTime(float duration)
        {
            this.duration = duration;
        }

        /// <summary>
        ///     调用之前一定要手动指定倒计时的时间
        /// </summary>
        public void StartTimer()
        {
            startTime = Time.time;
            targetTime = startTime + duration;
            isActive = true;
        }

        public void StopTimer()
        {
            isActive = false;
        }

        public void RemoveActionListener()
        {
            MonoManager.Instance.RemoveUpdateListener(Tick);
        }


        /// <summary>
        ///     event启动
        ///     默认配置在MonoManager中的update中
        ///     也可以自己手动启动
        /// </summary>
        public void Tick()
        {
            if (!isActive) return;
            if (Time.time > targetTime) OnTimerDone?.Invoke();
        }
    }

    /// <summary>
    ///     一次性倒计时
    ///     <para>使用完成后就可以丢掉</para>
    /// </summary>
    public class OnceTimer
    {
        private readonly float duration;

        private bool isActive;

        private float startTime;

        private float targetTime;

        /// <summary>
        ///     需要手动对事件进行监听
        /// </summary>
        /// <param name="duration"></param>
        public OnceTimer(float duration)
        {
            this.duration = duration;
        }

        /// <summary>
        ///     可以实现对事件的自动监听
        /// </summary>
        /// <param name="dutation"></param>
        /// <param name="onTimerDone"></param>
        public OnceTimer(float dutation, Action onTimerDone)
        {
            duration = dutation;
            OnTimerDone = onTimerDone;
        }

        public event Action OnTimerDone;

        public void StartTimer()
        {
            startTime = Time.time;
            targetTime = startTime + duration;
            isActive = true;
            //实践触发默认启动
            MonoManager.Instance.AddUpdateListener(Tick);
        }

        private void StopTimer()
        {
            isActive = false;
            MonoManager.Instance.RemoveUpdateListener(Tick);
        }


        /// <summary>
        ///     event启动
        ///     默认配置在MonoManager中的update中
        ///     也可以自己手动启动
        /// </summary>
        public void Tick()
        {
            if (!isActive) return;
            if (Time.time > targetTime)
            {
                OnTimerDone?.Invoke();
                StopTimer();
            }
        }
    }

    //mao 24-11-25 03:59
    /// <summary>
    ///     可以一开始设置多个倒计时
    /// </summary>
    public class MultipleTimer
    {
        private readonly List<float> _durations;

        private int curIndex;

        private bool isActive;

        private float startTime;

        private float targetTime;

        public MultipleTimer(List<float> durations, Action onTimerDone)
        {
            _durations = durations;
            curIndex = 0;
            OnTimerDone = onTimerDone;
        }

        public event Action OnTimerDone;

        public void ResetTimer()
        {
            ++curIndex;
            if (curIndex >= _durations.Count)
            {
                StopTimer();
                return;
            }

            //重新开启倒计时
            startTime = Time.time;
            targetTime = startTime + _durations[curIndex];
        }

        public void StartTimer()
        {
            startTime = Time.time;
            targetTime = startTime + _durations[curIndex];
            isActive = true;
            //实践触发默认启动
            MonoManager.Instance.AddUpdateListener(Tick);
        }

        private void StopTimer()
        {
            isActive = false;
            MonoManager.Instance.RemoveUpdateListener(Tick);
        }

        /// <summary>
        ///     event启动
        ///     默认配置在MonoManager中的update中
        ///     也可以自己手动启动
        /// </summary>
        public void Tick()
        {
            if (!isActive) return;
            if (Time.time > targetTime)
            {
                OnTimerDone?.Invoke();
                ResetTimer();
            }
        }
    }
}